Introducing CSS Grids

Unlock the power of layout with CSS Grid, where design meets limitless possibilities!

Made using background-attachment: fixed;

A grid example

This is item-1
This is item-2
This is item-3
This is item-4
This is item-5
This is item-6
This is item-7
This is item-8
This is item-9

What is a Grid?

CSS Grid is a powerful layout system in CSS that allows you to create complex two-dimensional grid layouts with ease. It introduces a new way to design web pages by dividing them into rows and columns. With CSS Grid, you can define a grid container and specify the size and alignment of its grid tracks. These tracks can be filled with grid items, such as elements or content areas, which can be positioned and ordered within the grid.

By using CSS Grid, you can achieve sophisticated and responsive layouts that adapt to different screen sizes and orientations without relying on complex CSS hacks or frameworks.

Grid Container - Using CSS Grid

To start using CSS Grid, you need to define a grid container. This can be any element in your HTML markup, such as a <div> or a <section>. Apply the CSS property display: grid; to the container to activate grid layout.

.grid-container { display: grid; }

Continued

You can specify the structure of your grid by setting the number and size of rows and columns. You can use various units like pixels (px), percentages (%), or flexible units like fractions (fr) to define the dimensions.

.grid-container { display: grid; /*Setting it to Grid*/ /* Make three columns. First of 300px, second of 100px, and third of 100px; */ grid-template-columns: 300px 100px 100px; /* auto to automatically fill the rest of the available space*/ grid-template-columns: 300px auto 100px; /* Three columns with a ratio of 1.2:2:4 */ grid-template-columns: 1.2fr 2fr 4fr; /* You can use the repeat function in CSS to make multiple columns */ grid-template-columns: repeat(3, auto); /* auto assigns equal width */ /* create gap between the grid cells */ grid-gap: 2rem; }

Another Example

This is box-1
This is box-2
This is box-3
This is box-4
This is box-5
.grid-container { display: grid; /* Using grid-template-rows*/ grid-template-rows: 1fr 3fr 2fr; /* default is 1fr */ grid-auto-rows: 2fr; /* change the default to 3fr */ /* Using grid-template-columns */ grid-template-columns: 1fr 4fr; grid-gap: 0.5rem; }

Another - Spanning across cells

item-1
item-2
item-3
item-4
item-5
item-6
item-7
item-8
item-9
item-10
item-11
item-12
item-13

Once you've defined the grid structure, you can position grid items within the grid. Grid items are the elements you want to place in the grid, such as <div> or <span> tags. Use the CSS property grid-row and grid-column to specify where the items should be placed.

Under the hood

#section-7 .box:first-child { /* Place the item in column 1 to 3 */ grid-column-start: 1; grid-column-end: 3; /* Place the item in row 1 to 5 */ grid-row-start: 1; grid-row-end: 5; } You can also use the Shorthand version: /* allows overlapping */ #section-7 .box:nth-child(8){ /* start/end */ grid-column: 2 / span 3; /* same as 2 / 5 */ grid-row: 4 / 6; }

minmax and auto-fit

Item-1
Item-2
Item-3
Item-4
Item-5
Item-6
Item-7
Item-8
Item-9
Item-10
Item-11
Item-12
Item-13
Item-14
Item-15

The elegant approach

#section-8 .grid-container{ ... display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); grid-gap: 1rem; justify-content: center; overflow-y: scroll; ... } #section-8 .grid-container::-webkit-scrollbar{ display: none; } #section-8 .box { ... aspect-ratio: 3/4; ... }

Grid Tempelate Areas - Reinventing Layouts intuitively

oiku klie poqe kied

Lorem ipsum dolor sit amet consectetur adipisicing elit. Eum quibusdam nesciunt animi saepe alias harum reprehenderit accusamus voluptates voluptatum, neque maxime commodi consequatur mollitia nisi quod itaque. Doloribus, alias soluta explicabo fuga delectus consequatur error dolorum minima vitae modi odio ea fugit illo tempora perspiciatis unde quo libero odit. Ratione quis sapiente voluptate maiores animi dolorum praesentium, accusantium quam molestiae? Tenetur quisquam, laudantium praesentium omnis sed non. A, possimus quas. Repudiandae culpa soluta, sequi laboriosam quod quis vel, beatae fugit, placeat facilis et iste aut quisquam. Accusantium dolores ex ut quam quis corporis expedita est iste a aliquam architecto ipsum, quod possimus nesciunt consequuntur. Explicabo ipsa neque eius, ea odio expedita sequi, animi tenetur nam sapiente consequatur corporis error, quo minus id dolor ullam cum asperiores ex excepturi aliquam. Aliquam error inventore sint ipsum ad recusandae, non possimus? Voluptatibus praesentium eveniet iusto incidunt sunt vero sint ipsa dolorum nam dolor. Reprehenderit, ex libero aperiam ut dolores accusamus eaque qui quidem tempore id porro. Ipsum atque facere sequi debitis officia neque veniam iure pariatur aut repudiandae eveniet provident illo voluptatibus dolor libero in, vel cupiditate optio deleniti aliquid eaque? Voluptatum, atque. Hic accusantium nulla dolores enim laboriosam amet sunt, dolorem omnis.

Ad Side Banner

How it works

Grid areas allow you to name regions of the grid, making it easier to place items within specific areas. You can then assign different grid-areas to different elements. This means that the order of the elements in the html document doesn't matter as they will be placed in their assigned grid-areas.

.grid { display: grid; grid-gap: 1rem; /* We can list what elements will fill what cells using grid-template-areas */ grid-template-areas: /* Fill the first row completely with navbar. */ 'navbar navbar navbar navbar navbar' /* Fill with main four-fifths of the way */ 'main main main main aside' 'footer footer footer footer footer'; grid-template-rows: 1fr 9fr 1fr; } .navbar { grid-area: navbar; } .main { grid-area: main; } .aside { grid-area: aside; } .footer { grid-area: footer; }